package com.eastfair.notice.controller;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.eastfair.constant.BusinessConstant;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.eastfair.core.exception.code.BaseExceptionCode;
import com.eastfair.core.exception.code.ExceptionCode;
import com.eastfair.core.utils.ConvertUtil;
import com.eastfair.notice.dto.CommentQuery;
import com.eastfair.notice.dto.CommentSaveDTO;
import com.eastfair.notice.dto.CommentUpdateDTO;
import com.eastfair.notice.entity.Comment;
import com.eastfair.notice.service.CommentService;
import com.eastfair.boot.request.PageParams;
import com.eastfair.core.context.ContextUtil;
import com.eastfair.notice.dto.CommentPageQuery;

import java.util.*;
import java.util.stream.Collectors;
import com.eastfair.boot.controller.SuperController;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.eastfair.echo.core.EchoService;
import com.eastfair.core.base.R;
import com.eastfair.notice.vo.CommentVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


/**
 * <p>
 * 前端控制器
 * 评论回复表
 * </p>
 *
 * @author liuxiaobai
 * @date 2021-08-13
 */
@Slf4j
@Validated
@RestController
@RequestMapping("/comment")
@Api(value = "Comment", tags = "评论回复表")
public class CommentController extends SuperController<CommentService, Long, Comment, CommentPageQuery, CommentSaveDTO, CommentUpdateDTO> {

    @Autowired
    private EchoService echoService;

    @Override
    public void handlerResult(IPage<Comment> page) {
        // 想让返回值实现自动回显，请将此行代码打开
        // echoService.action(page);
    }
    /**
     * Excel导入后的操作
     *
     * @param list
     */
    @Override
    public R<Boolean> handlerImport(List<Map<String, String>> list){
        List<Comment> commentList = list.stream().map((map) -> {
            Comment comment = Comment.builder().build();
            //TODO 请在这里完成转换
            return comment;
        }).collect(Collectors.toList());

        return R.success(baseService.saveBatch(commentList));
    }

    /**
     * 保存回复
     * @param saveDTO 回复实体
     * @return 主键ID
     */
    @RequestMapping("/save")
    @ApiOperation(value = "保存回复",notes = "保存回复")
    public R<Long> saveComment(@RequestBody @Validated CommentSaveDTO saveDTO) {
        Comment comment = BeanUtil.toBean(saveDTO,Comment.class);
        comment.setProjectId(ContextUtil.getProjectId());
        baseService.saveComment(comment);
        return success(comment.getId());
    }

    /**
     * 分页获取评论
     * @param params 分页入参
     * @return 分页数据
     */
    @RequestMapping("/queryPage")
    @ApiOperation(value = "分页获取",notes = "分页获取")
    public R<IPage<CommentVO>> queryPage(@RequestBody @Validated(CommentQuery.CommentValid.class) PageParams<CommentQuery> params) {
        if (params.getModel().getSubjectId() == null) {
            return fail(ExceptionCode.ILLEGAL_ARGUMENT_EX);
        }
        QueryWrapper<Comment> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda()
                .eq(Comment::getProjectId,ContextUtil.getProjectId())
                //评论所属模块类型
                .eq(Comment::getSubjectType,params.getModel().getSubjectType())
                //评论所属模块ID
                .eq(Comment::getSubjectId,params.getModel().getSubjectId())
                .eq(Comment::getIsDelete, BusinessConstant.DELETE_NO);
        Page<Comment> page = baseService.page(params.buildPage(),queryWrapper);
        Page<CommentVO> voPage = ConvertUtil.convertPage(page,CommentVO.class);
        List<CommentVO> list = voPage.getRecords();
        //组装姓名
        this.baseService.assembleComment(list);
        voPage.setRecords(list);
        return success(voPage);
    }

    /**
     * 时间正序获取回复list
     * @param query
     * @return
     */
    @RequestMapping("/queryList")
    @ApiOperation(value = "时间正序获取回复list",notes = "时间正序获取回复list")
    public R<List<CommentVO>> queryList(CommentQuery query) {
        //业务id和idList需要至少有一个
        if (query.getSubjectId() == null
                && (query.getSubjectIds() == null || query.getSubjectIds().isEmpty())) {
            return fail(ExceptionCode.ILLEGAL_ARGUMENT_EX);
        }
        QueryWrapper<Comment> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda()
                .eq(Comment::getProjectId,ContextUtil.getProjectId())
                //评论所属模块类型
                .eq(Comment::getSubjectType,query.getSubjectType())
                //评论所属模块ID
                .eq(query.getSubjectId() != null,Comment::getSubjectId,query.getSubjectId())
                .in(query.getSubjectIds() != null && !query.getSubjectIds().isEmpty(),Comment::getSubjectId,query.getSubjectIds())
                .eq(Comment::getIsDelete, BusinessConstant.DELETE_NO);
        if (query.getShortList() != null && query.getShortList()) {
            //获取短列表
            queryWrapper.last("limit 5");
        }
        List<Comment> commentList = this.baseService.list(queryWrapper);
        List<CommentVO> commentVOList = ConvertUtil.convertList(commentList,CommentVO.class);
        //组装姓名
        this.baseService.assembleComment(commentVOList);
        return success(commentVOList);
    }

}
